package edu.mobbuzz.page.authorization;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.FlowLayout;
import edu.mobbuzz.facade.RegisterFacade;
import edu.mobbuzz.messaging.Messaging;
import edu.mobbuzz.page.menu.InboxMenu;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.util.string.MenuString;
import java.io.IOException;

public class RegisterForm extends Menu {
    private Form registerForm;
    private Container registerPanel;
    private Container buttonPanel;
    private Label menuTitle;     
    private Label usernameLbl;
    private Label passwordLbl;
    private TextField usernameTxt;
    private TextField passwordTxt;
    private Command backCmd;
    private Button loginBtn;
    private Messaging messaging;
    private RegisterFacade registerFacade;
    
    public RegisterForm(){
        messaging = new Messaging();
        
        registerForm = new Form();
        registerPanel = new Container();
        menuTitle = new Label(getMenuTitle());

        usernameLbl = new Label();
        passwordLbl = new Label();

        usernameTxt = new TextField();
        passwordTxt = new TextField();

        loginBtn = new Button();
        buttonPanel = new Container(new FlowLayout(Component.LEFT));
        
        backCmd = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
                backToLoginPage();
            }
        };
    }
    
    public void showPage() {
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);

        usernameLbl.getStyle().setMargin(2, 10, 5, 2);
        usernameLbl.setText("Username");
        usernameLbl.getStyle().setMargin(2, 2, 5, 2);
        usernameLbl.getStyle().setBgTransparency(0);
        usernameTxt.getStyle().setMargin(2, 2, 5, 10);

        passwordLbl.setText("Password");
        passwordLbl.getStyle().setMargin(2, 2, 5, 2);
        passwordLbl.getStyle().setBgTransparency(0);
        passwordTxt.setConstraint(TextField.PASSWORD);
        passwordTxt.getStyle().setMargin(2, 2, 5, 10);

        loginBtn.setText("Register");
        loginBtn.getStyle().setPadding(5, 5, 7, 7);
        buttonPanel.addComponent(loginBtn);
        buttonPanel.getStyle().setMargin(2, 2, 2, 2);
        
        registerPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        registerPanel.addComponent(menuTitle);
        registerPanel.addComponent(usernameLbl);
        registerPanel.addComponent(usernameTxt);
        registerPanel.addComponent(passwordLbl);
        registerPanel.addComponent(passwordTxt);
        registerPanel.addComponent(buttonPanel);

        registerForm.setLayout(new BorderLayout());
        registerForm.addComponent(BorderLayout.CENTER, registerPanel);
        registerForm.addCommand(getExitCmd());
        registerForm.addCommand(backCmd);
        registerForm.setCommandListener(getActionlistener());
        loginBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if(usernameTxt.getText()==null||usernameTxt.getText().equals("")||
                   passwordTxt.getText()==null||passwordTxt.getText().equals("")){
                    Dialog.show("Error", "Username and password must not be left blank", Dialog.TYPE_ERROR, null, "Ok", null);
                    //connectToServer();
                }else{
                    
                    connectToServer();
                }

            }
        });
        registerForm.show();
    }

    public void backToLoginPage(){
        LoginPage loginPage = new LoginPage();
        loginPage.setExitCmd(registerForm.getBackCommand());
        loginPage.setActionlistener(getActionlistener());
        loginPage.showPage();
    }


    private void connectToServer() {
        Thread t = new Thread() {
			public void run() {
				try {
                    //System.out.println("connecting");
					//messaging.connect("mobbuzzworld.s155.eatj.com","client","mobbuzz.com","ada",0);
                    //messaging.connect("127.0.0.1","client","mobbuzz.com","ada",0);
                    //messaging.connect("circus.s155.eatj.com",usernameTxt.getText(),"mobbuzz.com",passwordTxt.getText(),0);
                   messaging.connect("jabber09.s46.eatj.com",usernameTxt.getText(),"mobbuzz.com",passwordTxt.getText(),0, getActionlistener(), registerForm.getBackCommand());
                   //messaging.connect("127.0.0.1",usernameTxt.getText(),"mobbuzz.com",passwordTxt.getText(),0, getActionlistener(), registerForm.getBackCommand());
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
        };

        t.start();
        do{

        }while(messaging.getState()!= messaging.ONLINE);
        if(messaging.getState()==messaging.ONLINE){
            register();
            try {
                openInbox(messaging);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    private void register(){
        edu.mobbuzz.bean.Login login = new edu.mobbuzz.bean.Login();
        login.setUsername(usernameTxt.getText());
        login.setPassword(passwordTxt.getText());
        login.setResources("mobile");

        getRegisterFacade().register(login);

    }
    private void openInbox(Messaging _messaging) {
        InboxMenu inboxMenu = new InboxMenu();
        inboxMenu.setExitCmd(getExitCmd());
        inboxMenu.setMessaging(_messaging);
        inboxMenu.setActionlistener(getActionlistener());
        inboxMenu.showPage();
    }

    public String getMenuTitle() {
        return MenuString.REGISTER;
    }

    public RegisterFacade getRegisterFacade() {
        if(registerFacade==null)
            registerFacade = new RegisterFacade();
        return registerFacade;
    }

}
 
